//! 动态生成器测试
//! 测试字段生成器类型功能

use dataforge::{
    db::schema::{TableSchema, DataType, FieldGeneratorType, FieldSchema, FieldConstraints},
    filling::generators::GenericDataGenerator
};
use serde_json::Value;

#[test]
fn test_dynamic_field_generator() {
    // 创建表结构
    let mut schema = TableSchema::new("test_table".to_string());
    
    // 添加使用默认生成器的字段
    schema.add_field(
        FieldSchema {
            name: "id".to_string(),
            data_type: DataType::Integer { min: Some(1), max: Some(1000000) },
            constraints: FieldConstraints::default(),
            description: None,
            generator_config: None,
            generator_type: None,
        }
    );
    
    // 添加使用特定生成器类型的字段
    schema.add_field(
        FieldSchema {
            name: "full_name".to_string(),
            data_type: DataType::String { max_length: Some(100) },
            constraints: FieldConstraints::default(),
            description: None,
            generator_config: None,
            generator_type: Some(FieldGeneratorType::Name),
        }
    );
    
    schema.add_field(
        FieldSchema {
            name: "email".to_string(),
            data_type: DataType::Email,
            constraints: FieldConstraints::default(),
            description: None,
            generator_config: None,
            generator_type: Some(FieldGeneratorType::RandomEmail),
        }
    );
    
    schema.add_field(
        FieldSchema {
            name: "phone".to_string(),
            data_type: DataType::Phone { country: Some("CN".to_string()) },
            constraints: FieldConstraints::default(),
            description: None,
            generator_config: None,
            generator_type: Some(FieldGeneratorType::RandomPhone),
        }
    );
    
    schema.add_field(
        FieldSchema {
            name: "company".to_string(),
            data_type: DataType::String { max_length: Some(100) },
            constraints: FieldConstraints::default(),
            description: None,
            generator_config: None,
            generator_type: Some(FieldGeneratorType::CompanyName),
        }
    );
    
    schema.add_field(
        FieldSchema {
            name: "created_at".to_string(),
            data_type: DataType::DateTime { format: None },
            constraints: FieldConstraints::default(),
            description: None,
            generator_config: None,
            generator_type: Some(FieldGeneratorType::CurrentTimestamp),
        }
    );
    
    // 生成数据
    let data = GenericDataGenerator::generate_data_by_schema(&schema, 3).unwrap();
    
    assert_eq!(data.len(), 3);
    
    for row in data {
        // 验证所有字段都存在
        assert!(row.contains_key("id"));
        assert!(row.contains_key("full_name"));
        assert!(row.contains_key("email"));
        assert!(row.contains_key("phone"));
        assert!(row.contains_key("company"));
        assert!(row.contains_key("created_at"));
        
        // 验证字段值类型正确
        assert!(row.get("id").unwrap().is_number());
        assert!(row.get("full_name").unwrap().is_string());
        assert!(row.get("email").unwrap().is_string());
        assert!(row.get("phone").unwrap().is_string());
        assert!(row.get("company").unwrap().is_string());
        assert!(row.get("created_at").unwrap().is_string());
        
        // 验证特定生成器类型的输出符合预期
        let full_name = row.get("full_name").unwrap().as_str().unwrap();
        assert!(!full_name.is_empty());
        
        let email = row.get("email").unwrap().as_str().unwrap();
        assert!(email.contains("@"));
        
        let phone = row.get("phone").unwrap().as_str().unwrap();
        assert!(phone.starts_with("1") && phone.len() == 11);
        
        let company = row.get("company").unwrap().as_str().unwrap();
        assert!(company.starts_with("Company "));
        
        let created_at = row.get("created_at").unwrap().as_str().unwrap();
        assert!(!created_at.is_empty());
    }
}

#[test]
fn test_default_generator_fallback() {
    // 创建表结构，不指定生成器类型，应该使用默认生成逻辑
    let mut schema = TableSchema::new("test_table".to_string());
    
    schema.add_field(
        FieldSchema {
            name: "id".to_string(),
            data_type: DataType::Integer { min: Some(1), max: Some(1000000) },
            constraints: FieldConstraints::default(),
            description: None,
            generator_config: None,
            generator_type: None,
        }
    );
    
    schema.add_field(
        FieldSchema {
            name: "name".to_string(),
            data_type: DataType::String { max_length: Some(100) },
            constraints: FieldConstraints::default(),
            description: None,
            generator_config: None,
            generator_type: None,
        }
    );
    
    schema.add_field(
        FieldSchema {
            name: "email".to_string(),
            data_type: DataType::Email,
            constraints: FieldConstraints::default(),
            description: None,
            generator_config: None,
            generator_type: None,
        }
    );
    
    // 生成数据
    let data = GenericDataGenerator::generate_data_by_schema(&schema, 1).unwrap();
    
    assert_eq!(data.len(), 1);
    
    let row = &data[0];
    
    // 验证所有字段都存在
    assert!(row.contains_key("id"));
    assert!(row.contains_key("name"));
    assert!(row.contains_key("email"));
    
    // 验证字段值类型正确
    assert!(row.get("id").unwrap().is_number());
    assert!(row.get("name").unwrap().is_string());
    assert!(row.get("email").unwrap().is_string());
}